package com.neuedu.dao.impl;

import com.neuedu.dao.BuildingsDao;
import com.neuedu.model.Buildings;
import com.neuedu.model.XiaoQu;
import com.neuedu.utils.DbUtil;

import java.util.ArrayList;
import java.util.List;

public class BuildingsDaoImpl implements BuildingsDao {
    @Override
    public List<Buildings> selectPage(String minDate, String maxDate, String likeName, Integer limitParam1, Integer limitParam2) {
        String where=" 1=1 ";
        List<Object> params=new ArrayList<>();
        if(minDate!=null&&!minDate.equals("")){
            where=where+"and chuangjianshijian >= ?";
            params.add(minDate);
        }
        if(maxDate!=null&&!maxDate.equals("")){
            where=where+"and chuangjianshijian <= ?";
            params.add(maxDate);
        }
        if(likeName!=null&&!likeName.equals("")){
            where=where+"and mingcheng like ?";
            params.add("%"+likeName+"%");
        }

        where=where+" order by id desc limit ?,?";
        params.add(limitParam1);
        params.add(limitParam2);

        List<Buildings> result = DbUtil.select(Buildings.class, where, params.toArray());

        return result;
    }

    @Override
    public int count(String minDate, String maxDate, String likeName) {
        String where=" 1=1 ";
        List<Object> params=new ArrayList<>();
        if(minDate!=null&&!minDate.equals("")){
            where=where+" and chuangjianshijian >= ?";
            params.add(minDate);
        }
        if(maxDate!=null&&!maxDate.equals("")){
            where=where+" and chuangjianshijian <= ?";
            params.add(maxDate);
        }
        if(likeName!=null&&!likeName.equals("")){
            where=where+"and mingcheng like ?";
            params.add("%"+likeName+"%");
        }
        //System.out.println(params);
        List<Buildings> result = DbUtil.select(Buildings.class, where, params.toArray());
        return result==null?0:result.size();
    }


    @Override
    public int insertBuildings(Buildings buildings) {
        return DbUtil.insertSelective(buildings);
    }

    @Override
    public int deleteBulidings(Integer[] ids) {
        String where ="id in (";
        for (int i = 0; i < ids.length; i++) {
            where=where+"?";
            if(i!=ids.length-1){
                where=where+",";
            }else{
                where=where+")";
            }
        }
        return DbUtil.delete(Buildings.class,where,ids);
    }

    @Override
    public Buildings selectBulidingsById(Integer id) {
        List<Buildings> list=DbUtil.select(Buildings.class,"id=?",id);
        if(list!=null&&list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public int updateBulidings(Buildings buildings) {
        return DbUtil.update(buildings,"id=?",buildings.getId());
    }
}
